<?php

namespace App\Admin\Controllers;

use App\FakeRank;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class FakeRankController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('虚拟区域排行设置')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new FakeRank);

        $grid->id('Id');
        $grid->type('类型')->display(function ($v) {
            return $GLOBALS['goods'][$v-1]->name;
        });
        $grid->dongbei('东北')->editable();
        $grid->xibei('西北')->editable();
        $grid->jingjin('京津')->editable();
        $grid->jinjimeng('晋冀蒙')->editable();
        $grid->shandong('山东')->editable();
        $grid->henan('河南')->editable();
        $grid->lianghu('两湖')->editable();
        $grid->suwan('苏皖')->editable();
        $grid->zhehu('浙沪')->editable();
        $grid->xinan('西南')->editable();
        $grid->yunguiguang('云贵广')->editable();
        $grid->yuehai('粤海')->editable();
        $grid->mingan('闽赣')->editable();
        $grid->column('订货合计(万箱)')->display(function ($v) {
           return set_decimal($this->getGoodsCount());
        });
        $grid->column('金额合计(万元)')->display(function () {
            $count = $GLOBALS['goods'][($this->id) - 1]->price * $this->getGoodsCount();
            return set_decimal($count);
        });
        $grid->footer(function () {
            $headers = ['单个区域合计','东北','西北','京津','晋冀蒙','山东','河南','两湖','苏皖','浙沪','西南','云贵广','粤海','闽赣','订货合计(万箱)	', '金额合计(万元)'];
            $fakers = FakeRank::all();
            $rows1 = ['订货量合计(万箱)'];
            $count1 = $this->getGoodsData($fakers);
            $rows1 = array_merge($rows1, $count1, [array_sum($count1)]);
            $rows2 = ['金额统计(万元)'];
            $count2 = $this->getMoneyCount($fakers);
            $rows2 = array_merge($rows2, $count2, [0], [array_sum($count2)]);

            $table = new Table($headers, [$rows1, $rows2]);
            return $table;
        });
        return $grid;
    }

    protected function getGoodsData($fakers)
    {
        $rows = [];
        $data = [
            'dongbei' => 0,
            'xibei' => 0,
            'jingjin' => 0,
            'jinjimeng' => 0,
            'shandong' => 0,
            'henan' => 0,
            'lianghu' => 0,
            'suwan' => 0,
            'zhehu' => 0,
            'xinan' => 0,
            'yunguiguang' => 0,
            'yuehai' => 0,
            'mingan' => 0,
        ];
        foreach ($fakers as $faker) {
            $data['dongbei'] += $faker->dongbei;
            $data['xibei'] += $faker->xibei;
            $data['jingjin'] += $faker->jingjin;
            $data['jinjimeng'] += $faker->jinjimeng;
            $data['shandong'] += $faker->shandong;
            $data['henan'] += $faker->henan;
            $data['lianghu'] += $faker->lianghu;
            $data['suwan'] += $faker->suwan;
            $data['zhehu'] += $faker->zhehu;
            $data['xinan'] += $faker->xinan;
            $data['yunguiguang'] += $faker->yunguiguang;
            $data['yuehai'] += $faker->yuehai;
            $data['mingan'] += $faker->mingan;
        }
        foreach ($data as $datum){
            array_push($rows, set_decimal($datum));
        }
        return $rows;
    }

    protected function getMoneyCount($fakers)
    {
        $rows = [];
        $data = [
            'dongbei' => 0,
            'xibei' => 0,
            'jingjin' => 0,
            'jinjimeng' => 0,
            'shandong' => 0,
            'henan' => 0,
            'lianghu' => 0,
            'suwan' => 0,
            'zhehu' => 0,
            'xinan' => 0,
            'yunguiguang' => 0,
            'yuehai' => 0,
            'mingan' => 0,
        ];
        foreach ($fakers as $key => $faker) {
            $data['dongbei'] += $faker->dongbei * $GLOBALS['goods'][$key]->price;
            $data['xibei'] += $faker->xibei * $GLOBALS['goods'][$key]->price;
            $data['jingjin'] += $faker->jingjin * $GLOBALS['goods'][$key]->price;
            $data['jinjimeng'] += $faker->jinjimeng * $GLOBALS['goods'][$key]->price;
            $data['shandong'] += $faker->shandong * $GLOBALS['goods'][$key]->price;
            $data['henan'] += $faker->henan * $GLOBALS['goods'][$key]->price;
            $data['lianghu'] += $faker->lianghu * $GLOBALS['goods'][$key]->price;
            $data['suwan'] += $faker->suwan * $GLOBALS['goods'][$key]->price;
            $data['zhehu'] += $faker->zhehu * $GLOBALS['goods'][$key]->price;
            $data['xinan'] += $faker->xinan * $GLOBALS['goods'][$key]->price;
            $data['yunguiguang'] += $faker->yunguiguang * $GLOBALS['goods'][$key]->price;
            $data['yuehai'] += $faker->yuehai * $GLOBALS['goods'][$key]->price;
            $data['mingan'] += $faker->mingan * $GLOBALS['goods'][$key]->price;
        }
        foreach ($data as $datum){
            array_push($rows, set_decimal($datum));
        }
        return $rows;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(FakeRank::findOrFail($id));

        $show->id('Id');
        $show->type('Type');
        $show->dongbei('Dongbei');
        $show->xibei('Xibei');
        $show->jingjin('Jingjin');
        $show->jinjimeng('Jinjimeng');
        $show->shandong('Shandong');
        $show->henan('Henan');
        $show->lianghu('Lianghu');
        $show->suwan('Suwan');
        $show->zhehu('Zhehu');
        $show->xinan('Xinan');
        $show->yunguiguang('Yunguiguang');
        $show->yuehai('Yuehai');
        $show->mingan('Mingan');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new FakeRank);

        $form->number('type', 'Type');
        $form->number('dongbei', 'Dongbei');
        $form->number('xibei', 'Xibei');
        $form->number('jingjin', 'Jingjin');
        $form->number('jinjimeng', 'Jinjimeng');
        $form->number('shandong', 'Shandong');
        $form->number('henan', 'Henan');
        $form->number('lianghu', 'Lianghu');
        $form->number('suwan', 'Suwan');
        $form->number('zhehu', 'Zhehu');
        $form->number('xinan', 'Xinan');
        $form->number('yunguiguang', 'Yunguiguang');
        $form->number('yuehai', 'Yuehai');
        $form->number('mingan', 'Mingan');

        return $form;
    }
}
